import {
  getMessageTemplateTable,
  editMessageTemplateRecord
} from '../../api/content'
import {
  MESSAGE_TEMPLATE_LIST,
  MESSAGE_TEMPLATE_DATA_COUNT,
  MESSAGE_TEMPLATE_CURRENT_ROW,
  MESSAGE_TEMPLATE_HISTORY_DATA,
  MESSAGE_TEMPLATE_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    messageTemplateList: [],
    messageTemplateDataCount: null,
    messageTemplatePageSize: 20,
    messageTemplateHistoryData: [],
    row: {
      title: '',
      state: '',
      type: '',
      ctime: '',
      operater: '',
      content: '',
      url: ''
    }
  },
  getters: {
    messageTemplateList: state => state.messageTemplateList
  },
  mutations: {
    [MESSAGE_TEMPLATE_LIST] (state, data) {
      state.messageTemplateList = data
    },
    [MESSAGE_TEMPLATE_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [MESSAGE_TEMPLATE_DATA_COUNT] (state, data) {
      state.messageTemplateDataCount = data
    },
    [MESSAGE_TEMPLATE_HISTORY_DATA] (state, data) {
      state.messageTemplateHistoryData = data
    },
    [MESSAGE_TEMPLATE_PAGE_SIZE] (state, data) {
      state.messageTemplatePageSize = data
    }
  },
  actions: {
    async getMessageTemplateList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getMessageTemplateTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          title: item.title,
          state: item.state,
          ctime: item.ctime,
          type: item.type,
          operater: item.ctime,
          content: item.content,
          url: item.url
        }]
      })
      commit(MESSAGE_TEMPLATE_LIST, newData)
      commit(MESSAGE_TEMPLATE_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.messageTemplate.messageTemplatePageSize) {
        commit(MESSAGE_TEMPLATE_HISTORY_DATA, newData)
      } else {
        commit(MESSAGE_TEMPLATE_HISTORY_DATA, newData.slice(0, rootState.messageTemplate.messageTemplatePageSize))
      }
      return resp
    },
    async editMessageTemplateData ({ commit, rootState }, playload) {
      // console.log(playload)
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editMessageTemplateRecord(data)
      return resp
    },
    async getMessageTemplateCurrentRow ({ commit, rootState }, row) {
      commit(MESSAGE_TEMPLATE_CURRENT_ROW, row)
    },
    async changeMessageTemplatePage ({ commit, rootState }, index) {
      let data = rootState.messageTemplate.messageTemplateList
      let _start = (index - 1) * rootState.messageTemplate.messageTemplatePageSize
      let _end = index * rootState.messageTemplate.messageTemplatePageSize
      commit(MESSAGE_TEMPLATE_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeMessageTemplatePageSize ({ commit, rootState }, value) {
      commit(MESSAGE_TEMPLATE_PAGE_SIZE, value)
    }
  }
}
